// pages/share/share.js
import drawQrcode from '../../utils/weapp.qrcode.min'
import {
    login,
} from '../../api/user.js'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        show: false,
        upID: null,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // drawQrcode({
        //     width: 200,
        //     height: 200,
        //     canvasId: 'myQrcode',
        //     text: 'https://www.baidu.com/',
        //   })
        if (options.id) {
            this.setData({
                show: true,
                upID: options.id
            })
        } else {
            this.setData({
                show: false
            })
        }
    },
    author(e) {
        if (e.detail.errMsg === 'getPhoneNumber:ok') {
            this.login(e);
        }
    },
    /**
     * 用户登录
     */
    login(e) {
        let _this = this
        wx.showLoading({
            title: '登录中',
        })
        var nick = null
        var avatarUrl = null
        wx.getUserInfo({
            success: res => {
                console.log(res, '用户信息')
                nick = res.userInfo.nickName
                avatarUrl = res.userInfo.avatarUrl
                wx.login({
                    timeout: 3000,
                    success: (res) => {
                        wx.setStorageSync('code', res.code);
                        login({
                            code: res.code,
                            encryptedData: e.detail.encryptedData,
                            iv: e.detail.iv,
                            upUser: this.data.upID,
                            nick: nick,
                            headUrl: avatarUrl
                        }).then(async res => {
                            wx.hideLoading()
                            if (res.content.user) {
                                wx.setStorage({
                                    key: 'token',
                                    data: res.content.token
                                })
                                wx.setStorage({
                                    key: 'user',
                                    data: res.content.user
                                })
                                wx.setStorage({
                                    key: 'isSimpleCourse',
                                    data: res.content.isSimpleCourse
                                })
                                if (wx.getStorageSync('path')) {
                                    wx.reLaunch({
                                        url: wx.getStorageSync('path'),
                                    })
                                } else {
                                    // 登录成功
                                    wx.reLaunch({
                                        url: '/pages/home/home',
                                    })
                                }
                            }
                        })

                    }
                })
            }
        })
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {
        return {
            title: '健享经纪',
            path: '/pages/jianjie/jianjie?id=' + wx.getStorageSync('user').userId
        }
    },
})